package com.unme.controller;

import com.alibaba.fastjson.JSONObject;
import com.unme.model.Msg;
import com.unme.service.MsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/apm")
public class MsgController {
    @Autowired
    private MsgService msgService;

    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/addMsg", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> addMsg(@RequestBody(required=true) JSONObject jsonParam, HttpServletResponse httpResponse, HttpServletRequest request) {

        Msg msg=new Msg();
        Map<String,Object> map=new HashMap<>();
        String oa=request.getParameter("oa");
        try{
            msg.setFormuser(jsonParam.getInteger("formuser"));
            msg.setTouser(jsonParam.getInteger("touser"));
            msg.setParentId(jsonParam.getInteger("parentId"));
            msg.setContent(jsonParam.getString("content"));
            msg.setCreatetime(jsonParam.getDate("createtime"));
            msgService.addMsg(msg);
            map.put("message","数据添加成功！");
            return map;
        }catch (Exception e){
            e.printStackTrace();
            map.put("message","数据添加失败！");
            return map;

        }
    }


    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/findMsg", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> findMsg(@RequestBody(required=true) JSONObject jsonParam, HttpServletResponse httpResponse, HttpServletRequest request) {

        Msg msg=new Msg();
        Map<String,Object> map=new HashMap<>();
        String oa=request.getParameter("oa");
        try{
            map.put("data",msgService.findMsg(msg));
            map.put("message","数据查询成功！");

            return map;
        }catch (Exception e) {
            e.printStackTrace();
            map.put("message", "数据查询失败！");
            return map;
        }
    }
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/deleteMsg", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> deleteMsg(@RequestBody(required=true) JSONObject jsonParam, HttpServletResponse httpResponse, HttpServletRequest request) {

        Msg msg=new Msg();
        Map<String,Object> map=new HashMap<>();
        String oa=request.getParameter("oa");
        try{

            msgService.deleteMsg(msg);
            map.put("message","数据删除成功！");
            return map;
        }catch (Exception e){
            e.printStackTrace();
            map.put("message","数据删除失败！");
            return map;

        }
    }
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/updateMsg", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> updateMsg(@RequestBody(required=true) JSONObject jsonParam, HttpServletResponse httpResponse, HttpServletRequest request) {

        Msg msg=new Msg();
        Map<String, Object> map = new HashMap<>();
        String oa = request.getParameter("oa");
        try {
            msg.setFormuser(jsonParam.getInteger("formuser"));
            msg.setTouser(jsonParam.getInteger("touser"));
            msg.setParentId(jsonParam.getInteger("parentId"));
            msg.setContent(jsonParam.getString("content"));
            msg.setCreatetime(jsonParam.getDate("createtime"));
            msgService.updateMsg(msg);
            map.put("message", "数据更新成功！");
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message", "数据更新失败！");
            return map;

        }
    }
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/getMsgById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> getMsgById(@RequestBody(required=true) JSONObject jsonParam, HttpServletResponse httpResponse, HttpServletRequest request) {

        Msg msg=new Msg();
        Map<String, Object> map = new HashMap<>();
        String oa = request.getParameter("oa");
        try {
            msg.setFormuser(jsonParam.getInteger("formuser"));
            msg.setTouser(jsonParam.getInteger("touser"));
            msg.setParentId(jsonParam.getInteger("parentId"));
            msg.setContent(jsonParam.getString("content"));
            msg.setCreatetime(jsonParam.getDate("createtime"));
            msgService.getMsgById(msg);
            map.put("message", "数据获取成功！");
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message", "数据获取失败！");
            return map;

        }
    }

}
